package com.boot.project.admin.controller.manage;

import com.boot.project.admin.service.AuthService;
import com.boot.project.core.annotation.ResponseResult;
import com.boot.project.core.enums.log.ParamPosition;
import com.boot.project.core.model.result.ProfileResult;
import com.boot.project.core.model.system.dto.UserLoginDto;
import com.boot.project.core.model.system.dto.UserPasswordDto;
import com.boot.project.module.log.annotation.AccessLog;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@ResponseResult
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录
     * @param dto 登录信息
     * @return
     */
    @PostMapping("/login")
    @AccessLog(value = "用户登录", position = ParamPosition.BODY)
    public void login(UserLoginDto dto) {
        authService.login(dto);
    }

    /**
     * 根据用户的标识查找对应的权限信息
     * @return
     */
    @GetMapping("/profile")
    @AccessLog(value = "查询用户自己的权限信息")
    public ProfileResult profile() {
        return authService.profile();
    }

    /**
     * 管理员重置用户密码，将密码重置为初始密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/resetPassword")
    @AccessLog(value = "重置用户密码", position = ParamPosition.BODY)
    public void resetPassword(@RequestBody UserPasswordDto userPasswordDto) {
        authService.resetPassword(userPasswordDto);
    }

    /**
     * 用户修改自己的密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/editPassword")
    @AccessLog(value = "用户修改密码", position = ParamPosition.BODY)
    public void editPassword(@RequestBody UserPasswordDto userPasswordDto) {
        ProfileResult result = (ProfileResult) SecurityUtils.getSubject().getPrincipal();
        userPasswordDto.setId(result.getId());
        authService.editPassword(userPasswordDto);
    }

    /**
     * 当用户密码为初始密码时，强制修改用户密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/forceEditPassword")
    @AccessLog(value = "强制用户修改密码", position = ParamPosition.BODY)
    public void forceEditPassword(@RequestBody UserPasswordDto userPasswordDto) {
        ProfileResult result = (ProfileResult) SecurityUtils.getSubject().getPrincipal();
        userPasswordDto.setId(result.getId());
        authService.forceEditPassword(userPasswordDto);
    }
}
